package movie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import movie.entity.Film;
import movie.service.FilmService;

@Controller
public class FilmController {
	@Autowired
	private FilmService filmService;
	@RequestMapping(value = "/films/{id}", method = RequestMethod.GET)
	public String get(@PathVariable("id") int fid, Model model) {
		Film f = filmService.find_film_by_id(fid);
		model.addAttribute("cur_film", f);
		model.addAttribute("wantsee", 0);
		model.addAttribute("flag_score", 0);
		return "Film/film_introduction";
	}
	
	@RequestMapping(value = "/wantsee/{id}", method = RequestMethod.GET)
	public String get1(@PathVariable("id") int fid, Model model) {
		Film f = filmService.find_film_by_id(fid);
		f.setWantsee(f.getWantsee() + 1);
		filmService.updatewantsee(f);
		model.addAttribute("cur_film", f);
		model.addAttribute("wantsee", 1);
		model.addAttribute("flag_score", 0);
		return "Film/film_introduction";
	}
	
	@RequestMapping(value = "/scoreNumber", method = RequestMethod.POST)
	public @ResponseBody Film post2(Film film, Model model) {
		
		Film f = filmService.find_film_by_id(film.getMovieId());
		float tmp = (film.getScore() + f.getScoreNumber() * f.getScore()) / (f.getScoreNumber() + 1);
		float tmp1 = (float)(Math.round(tmp*10))/10;
		f.setScore(tmp1);
		f.setScoreNumber(f.getScoreNumber() + 1);
		System.out.println(f.getScoreNumber());
		filmService.updatescore(f);
		return f;
	}
	
	@RequestMapping(value = "/findfilms_byname", method = RequestMethod.GET)
	public String getfind(String name, Model model) {
		model.addAttribute("find_films", filmService.findbyname(name));
		return "/Film/find_films";
	}
}
